﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class Misiuni : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

        base.InitializeCulture();
    }

    private bool CheckServerObject()
    {
        bool result = false;

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }
        else
        {
            mServerObject = new ServerObject();
            Session["ServerModule"] = mServerObject;
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    protected void FillCounryCountryCombo()
    {
        DataTable categoryList = mServerObject.GetCountryList();
        mServerObject.fillSelector(countryDDL, categoryList, "DenumireJudet", "IdJudet");
    }   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }

        if (CheckServerObject())
        {
            if (!IsPostBack)
            {
                FillCounryCountryCombo();
            }
        }
        else
        {
            Response.Redirect("home.aspx", true);
        }
    }
     
    protected void countryDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearInfo();

        int idTara = 0;
        int.TryParse(countryDDL.SelectedValue, out idTara);

        DataTable judeteList = mServerObject.GetJudeteList(idTara);            
        mServerObject.fillFrontPageSelector(judeteDDL, judeteList, "DenumireJudet", "IdJudet");
        if (judeteList == null || judeteList.Rows.Count == 0)
        {
            DataTable consulatInfo = mServerObject.GetConsulatListByCountryID(idTara);
            if (consulatInfo != null && consulatInfo.Rows.Count == 1)
            {
                idMisiuneaHiddentField.Value = consulatInfo.Rows[0]["idConsulat"].ToString();
                nameConsulatLabel.Text = consulatInfo.Rows[0]["Denumire"].ToString();
            }

            GetInfo();
        }        
    }

    protected void ClearInfo()
    {
        idMisiuneaHiddentField.Value = string.Empty;
        nameConsulatLabel.Text = "---";
        adresaTextBox.Text = string.Empty;
        telTextBox.Text = string.Empty;
        faxTextBox.Text = string.Empty;
        emailTextBox.Text = string.Empty;
        webTextBox.Text = string.Empty;        
    }

    protected void consulatDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetInfo();
    }

    protected void GetInfo()
    {
        int idConsulat = 0;
        int.TryParse(idMisiuneaHiddentField.Value, out idConsulat);

        DataTable infoTable = mServerObject.GetConsulatInfoByID(idConsulat);

        if (infoTable != null && infoTable.Rows.Count == 1)
        {
            adresaTextBox.Text = infoTable.Rows[0]["adresa"].ToString();
            telTextBox.Text = infoTable.Rows[0]["telefon"].ToString();
            faxTextBox.Text = infoTable.Rows[0]["fax"].ToString();
            emailTextBox.Text = infoTable.Rows[0]["email"].ToString();
            webTextBox.Text = infoTable.Rows[0]["web"].ToString();
        }
    }

    protected void judeteDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearInfo();

        int idTara = 0;
        int.TryParse(judeteDDL.SelectedValue, out idTara);

        DataTable consulatInfo = mServerObject.GetConsulatListByCountryID(idTara);
        if (consulatInfo != null && consulatInfo.Rows.Count == 1)
        {
            idMisiuneaHiddentField.Value = consulatInfo.Rows[0]["idConsulat"].ToString();
            nameConsulatLabel.Text = consulatInfo.Rows[0]["Denumire"].ToString();
        }

        GetInfo();
    }
}